/*
utf-8
------------------------------------------------
功能选择程序
------------------------------------------------
由刘丙旭出品
遵循署名-非商业性使用-相同方式共享 4.0 国际协议 (CC BY-NC-SA 4.0)
详情访问https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh
------------------------------------------------
*/

module clock_sel(
input MODE,
input SETBIT,
input SET,
input RESET,
input clk,

output reg [1:0] MODE_sel,
output reg [1:0] SETBIT_sel,
output reg [1:0] SET_sel,
output reg [1:0] RESET_sel,
output reg [3:0] LED,
output reg [1:0] count
);

	
	wire RESET_ax,MODE_ax,SETBIT_ax,SET_ax;
	//reg [1:0] count;		//0：计时；1：校时；2：闹钟设置
	
	always @(negedge MODE_ax)
	begin
		if (count == 2'd2)
			begin count <= 2'd0;  end
		else
			begin count <= count + 1; end
			
	end
	
	always @(negedge clk)
	case(count)
		2'b00:begin
					MODE_sel 		<= 2'b11;
					SETBIT_sel 	    <= 2'b11;
					SET_sel 		<= 2'b11;
					RESET_sel 		<= 2'b11;
					LED				<= 4'b0000;
					end
		2'b01:begin
					MODE_sel 		<= 2'b01;
					SETBIT_sel 		<= {SETBIT_ax,1'b1};
					SET_sel 		<= {SET_ax,1'b1};
					RESET_sel 		<= {RESET_ax,1'b1};
					LED				<= 4'b1000;
					end
		2'b10:begin
					MODE_sel 		<= 2'b10;
					SETBIT_sel 		<= {1'b1,SETBIT_ax};
					SET_sel 		<= {1'b1,SET_ax};
					RESET_sel 		<= {1'b1,RESET_ax};
					LED				<= 4'b0100;
					end
		default begin
					MODE_sel 		<= 2'b11;
					SETBIT_sel 		<= 2'b11;
					SET_sel 		<= 2'b11;
					RESET_sel 		<= 2'b11;
					LED				<= 4'b0000;
					end
	endcase
	
	
	ax_debounce ax_debounce_m0		//按键消抖
	(		
	.clk             (clk),
	.rst             (1'b0),
	.button_in       (RESET),
	.button_posedge  (),
	.button_negedge  (),
	.button_out      (RESET_ax)
	);
	
	
	ax_debounce ax_debounce_m1		//按键消抖
	(		
	.clk             (clk),
	.rst             (1'b0),
	.button_in       (MODE),
	.button_posedge  (),
	.button_negedge  (),
	.button_out      (MODE_ax)
	);
	
	
	ax_debounce ax_debounce_m2		//按键消抖
	(		
	.clk             (clk),
	.rst             (1'b0),
	.button_in       (SETBIT),
	.button_posedge  (),
	.button_negedge  (),
	.button_out      (SETBIT_ax)
	);
	
	
	ax_debounce ax_debounce_m3		//按键消抖
	(		
	.clk             (clk),
	.rst             (1'b0),
	.button_in       (SET),
	.button_posedge  (),
	.button_negedge  (),
	.button_out      (SET_ax)
	);
	
endmodule